﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Domain.Model;
using Domain.Model.Interfaces;

using NHibernate;

using DataAccess.Utils;
using DataAccess.Exceptions;

namespace DataAccess.Repositories
{
    public class FormRepository : AbstractRepository<Form>, IFormRepository
    {
        public FormRepository() : base(DataRepositoryException.FORM_REPOSITORY, "form") { }

        public void DeleteFormElement(FormElement entity)
        {
            using (ISession session = SessionProvider.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        session.Delete(entity);
                        session.Flush();
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();

                        throw new DataRepositoryException(e.Message,
                            DataRepositoryException.FORM_REPOSITORY,
                            "Could not delete the form element: " + entity.Name);
                    }
                }
            }
        }
    }
}